package com.application.DataProvider;

import java.util.ArrayList;

import com.application.DTO.PartDTO;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class PartDAO {

	Context _mcontext = null;

	public PartDAO(Context context){
		this._mcontext = context;
	}

	public boolean isExist(int id)
	{
		Log.d("PEERDROID","id part : " + id);
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		boolean result = false;
		Cursor cur = null;
		try 
		{
			String sql = "Select count(*) from Part where ID=" +  id;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				int i = Integer.valueOf(cur.getString(0));
				if(i != 0)
					result = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		Log.d("PEERDROID","id part: " + result);
		return result;
	}

	public String getName(int id)
	{
		String name = null;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select name from part where ID = " + id;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				name = cur.getString(0);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		return name;

	}
	public int getIDPartPre(int id, int idChap)
	{
		int idPre;
		Log.d("PEERDROID","id part:" + id);
		Log.d("PEERDROID","id chap:" + idChap);
		ArrayList<Integer> idList = new ArrayList<Integer>();
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select id from part where IDChapter = " + idChap;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				idList.add(cur.getInt(0));
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		idPre = 0;
		Log.d("PEERDROID","id list:" + idList.toString());
		for(int i = 0; i < idList.size(); i++)
		{
			if(idList.get(i) == id)
				break;
			idPre = idList.get(i);
		}
		return idPre;

	}

	public int getIDPartNext(int id, int idChap)
	{
		Log.d("PEERDROID","id part:" + id);
		Log.d("PEERDROID","id chap:" + idChap);
		int idNext;
		ArrayList<Integer> idList = new ArrayList<Integer>();
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select id from part where IDChapter = " + idChap;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				idList.add(cur.getInt(0));
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		Log.d("PEERDROID","id list:" + idList.toString());
		idNext = -1;
		for(int i = idList.size() - 1; i >=0 ; i--)
		{
			if(idList.get(i) == id)
				break;
			idNext = idList.get(i);
		}
		return idNext;
	}
	public ArrayList<Integer> getIDPartList(int idChap)
	{
		ArrayList<Integer> idList = new ArrayList<Integer>();
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select id from part where IDChapter = " + idChap;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				idList.add(cur.getInt(0));
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		return idList;
	}
	public ArrayList<PartDTO> getPartList(int idChap)
	{
		ArrayList<PartDTO> partList = new ArrayList<PartDTO>();

		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select * from part where IDChapter = " + idChap;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				PartDTO pDto = new PartDTO();
				pDto.set_ID(Integer.valueOf(cur.getString(0)));
				pDto.set_Name(cur.getString(1));
				pDto.set_Note(cur.getString(2));
				pDto.set_IDChapter(Integer.valueOf(cur.getString(3)));
				pDto.set_Content(cur.getString(4));
				partList.add(pDto);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return partList;

	}

	public ArrayList<PartDTO> getPartListFeeback(int idChap)
	{
		ArrayList<PartDTO> partList = new ArrayList<PartDTO>();

		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select * from part where IDChapter = " + idChap;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				PartDTO pDto = new PartDTO();
				pDto.set_ID(Integer.valueOf(cur.getString(0)));
				pDto.set_Name(cur.getString(1));
				pDto.set_Note(cur.getString(2));
				pDto.set_IDChapter(Integer.valueOf(cur.getString(3)));
				pDto.set_Content(cur.getString(4));
				partList.add(pDto);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return partList;

	}
	public PartDTO getPart(int IDPart)
	{
		PartDTO pDto = new PartDTO();
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select * from part where ID = " + IDPart;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				pDto.set_ID(Integer.valueOf(cur.getString(0)));
				pDto.set_Name(cur.getString(1));
				pDto.set_Note(cur.getString(2));
				pDto.set_IDChapter(Integer.valueOf(cur.getString(3)));
				pDto.set_Content(cur.getString(4));
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return pDto;
	}
	public boolean getStatus(int idPart)
	{
		boolean result = false;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select Status from Part where ID = " + idPart;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				if(cur.getString(0).equals("1"))
					result = true;

			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		Log.d("PEERDROID","Result status : " + result);
		return result;
	}

	public int getIDChap(int idPart)
	{
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select IDChapter from Part where ID = " + idPart;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				result = cur.getInt(0);

			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		Log.d("PEERDROID","Result status : " + result);
		return result;
	}
	//Insert du lieu vao bang Part
	public int insertPart(PartDTO pDto) {
		int result = 0;
		Log.d("PEERDROID","insert part");
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			ContentValues values = new ContentValues();
			values.put("ID", pDto.get_ID());
			values.put("Name", pDto.get_Name());
			values.put("Note", pDto.get_Note());
			values.put("IDChapter", pDto.get_IDChapter());
			values.put("Content", pDto.get_Content());
			values.put("Status", pDto.isNew());

			result = provider.execInsertCommand(db, "Part", values);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}

	//Cap nhat bang Part
	public int updatePart(PartDTO ct_dto)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{
			ContentValues values = new ContentValues();
			values.put("Name", ct_dto.get_Name());
			values.put("Note", ct_dto.get_Note());
			values.put("IDChapter", ct_dto.get_IDChapter());
			values.put("Content", ct_dto.get_Content());
			values.put("Status", ct_dto.isNew());

			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(ct_dto.get_ID())};
			result= provider.execUpdateCommand(db, "Part", values, whereClause, whereArgs);

		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}
		return result;
	}

	public int updatePart(boolean status, int idPart)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{
			ContentValues values = new ContentValues();
			values.put("Status", status);

			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(idPart)};
			result= provider.execUpdateCommand(db, "Part", values, whereClause, whereArgs);

		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}
		return result;
	}
	//Delete doi tuong PartDTO
	public int deletePart(int idPart)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{	
			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(idPart)};	
			result=provider.execDeleteCommand(db, "Part", whereClause, whereArgs);
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}
		return result;
	}
}
